package cn.edu.nju.software.cdiy.dao.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;

import cn.edu.nju.software.cdiy.algorithm.performance.RadiatorEvaluator;
import cn.edu.nju.software.cdiy.bean.BRD_RAD;
import cn.edu.nju.software.cdiy.bean.Brand;
import cn.edu.nju.software.cdiy.bean.CPU;
import cn.edu.nju.software.cdiy.bean.Hardware;
import cn.edu.nju.software.cdiy.bean.HardwareSet;
import cn.edu.nju.software.cdiy.bean.Interface;
import cn.edu.nju.software.cdiy.bean.MainBoard;
import cn.edu.nju.software.cdiy.bean.Power;
import cn.edu.nju.software.cdiy.bean.RAD_CompatibleCPU;
import cn.edu.nju.software.cdiy.bean.RAD_Way;
import cn.edu.nju.software.cdiy.bean.Radiator;
import cn.edu.nju.software.cdiy.bean.Value;
import cn.edu.nju.software.cdiy.dao.RadiatorDao;
import cn.edu.nju.software.cdiy.util.Changer;
import cn.edu.nju.software.cdiy.util.Constant;
import cn.edu.nju.software.cdiy.util.FilterItem;
import cn.edu.nju.software.cdiy.util.Item;
import cn.edu.nju.software.cdiy.util.NumberFormatter;
import cn.edu.nju.software.cdiy.util.Pager;
import cn.edu.nju.software.cdiy.util.SortByHotAsc;
import cn.edu.nju.software.cdiy.util.SortType;
import cn.edu.nju.software.cdiy.util.SortWay;

public class RadiatorDaoImpl implements RadiatorDao {
	
	@Autowired
	private SessionFactory sessionFactory;

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@Override
	public List<Radiator> getRadiatorList(int page, SortType s, HardwareSet hs,
			SortWay sw) {
		// System.out.println("进入daoimpl getmblist");
		Session sess = sessionFactory.getCurrentSession();
		String sql = null;
		List<Radiator> list = null;

		// 第page页的第minNum条记录到maxNum条记录
		int maxNum = Pager.getMax(page);
		int minNum = Pager.getMin(maxNum);

		switch (s) {
		case PRICE:
			if (sw == SortWay.ASC || sw == SortWay.DEFAULT)
				sql = "FROM Radiator WHERE STATE=882 ORDER BY OPRICE ASC";
			else
				sql = "FROM Radiator WHERE STATE=882 ORDER BY OPRICE DESC";
			list = sess.createQuery(sql).list();
			break;
		case PERFORMANCE:
			if (sw == SortWay.DESC || sw == SortWay.DEFAULT)
				sql = "FROM Radiator WHERE STATE=882 ORDER BY PERFORMANCE DESC";
			else
				sql = "FROM Radiator WHERE STATE=882 ORDER BY PERFORMANCE ASC";
			list = sess.createQuery(sql).list();
			break;
		case W_SCORE:
			if (sw == SortWay.DESC || sw == SortWay.DEFAULT)
				sql = "FROM Radiator WHERE STATE=882 ORDER BY WSCORE DESC";
			else
				sql = "FROM Radiator WHERE STATE=882 ORDER BY WSCORE ASC";
			list = sess.createQuery(sql).list();
			break;
		case Z_SCORE:
			if (sw == SortWay.DESC || sw == SortWay.DEFAULT)
				sql = "FROM Radiator WHERE STATE=882 ORDER BY ZSCORE DESC";
			else
				sql = "FROM Radiator WHERE STATE=882  ORDER BY ZSCORE ASC";
			list = sess.createQuery(sql).list();
			break;
		case NEW:
			if (sw == SortWay.DESC || sw == SortWay.DEFAULT)
				sql = "FROM Radiator WHERE STATE=882 ORDER BY INSTORAGETIME DESC";
			else
				sql = "FROM Radiator WHERE STATE=882 ORDER BY INSTORAGETIME ASC";
			list = sess.createQuery(sql).list();

			break;
		case HOT:
			sql = "FROM Radiator WHERE STATE=882";
			list = sess.createQuery(sql).list();
			if (sw == SortWay.DESC || sw == SortWay.DEFAULT)
				Collections.sort(list);
			else {
				Collections.sort(list, new SortByHotAsc());
			}
			break;
		default:
			break;

		}
		list = filterList(hs, list, minNum, maxNum);
		
		return list;
	}

	private List<Radiator> filterList(HardwareSet hs, List<Radiator> list,
			int min, int max) {
		/*
		 * 分别由各部件的filter对待检测的部件表进行筛选 目前只有二个条件可以用来筛选 MainBoard CPU
		 */
		if (hs != null) {
			list = MainBoardFilter(hs, list);
			list = CPUFilter(hs, list);
		}
		// 最终根据页数的要求返回list
		List<Radiator> returnList = new Pager<Radiator>().getPageList(list,
				min, max);
		return returnList;
	}

	private List<Radiator> CPUFilter(HardwareSet hs, List<Radiator> l) {
		/*
		 * CPU对散热器的限制
		 */
		if (hs.getCpu() != null) {
			CPU cpu = hs.getCpu();
			Session sess = sessionFactory.getCurrentSession();
			// 获取CPU的插槽类型
			int id = cpu.getSlotType().getId();

			// 返回的list
			List<Radiator> returnList = new ArrayList<Radiator>();

			for (int i = 0; i < l.size(); i++) {
				// 遍历原表进行筛选
				Radiator nowRad = l.get(i);
				// 获取散热器对应的使用CPU列表
				int Rid = nowRad.getId();
				String sql = "FROM RAD_CompatibleCPU WHERE RAD=" + Rid;
				List<RAD_CompatibleCPU> list = sess.createQuery(sql).list();

				for (int j = 0; j < list.size(); j++) {
					// 检测此散热器的使用CPU接口是否符合CPU的接口
					if (list.get(j).getCpu().getId() == id) {
						// 若符合要求则加入返回list 并跳出进行下一个散热器的检验
						returnList.add(nowRad);
						break;
					} else
						continue;
				}
			}
			
			return returnList;
		} else
			return l;
	}

	private List<Radiator> MainBoardFilter(HardwareSet hs, List<Radiator> l) {
		/*
		 * 主板对散热器的限制
		 */
		if (hs.getMainBoard() != null) {
			MainBoard mb = hs.getMainBoard();
			// 获取主板Size的长和宽
			double[] size1 = NumberFormatter
					.getLengthAndWidth(mb.getSizeOfMB());
			double length1 = size1[0];
			double width1 = size1[1];

			// 返回的list
			List<Radiator> returnList = new ArrayList<Radiator>();

			for (int i = 0; i < l.size(); i++) {
				// 遍历原表进行筛选
				Radiator nowRad = l.get(i);
				// 获取散热器Size的长和宽
				double[] size2 = NumberFormatter.getLengthAndWidth(nowRad
						.getrSize());
				double length2 = size2[0];
				double width2 = size2[1];
				// 散热器的长和宽要小于主板的长和宽
				if (length2 <= length1 && width2 <= width1) {
					// 若符合要求则加入返回list 并跳出进行下一个散热器的检验
					returnList.add(nowRad);
					break;
				}
			}
			return returnList;
		} else
			return l;
	}

	@Override
	public List<Hardware> getNewestList() {
		Session sess = sessionFactory.getCurrentSession();
		String sql = "FROM Radiator WHERE STATE=882 ORDER BY INSTORAGETIME DESC";
		List<Radiator> list = sess.createQuery(sql).list();
		if (list.size() > Constant.NEW_LIST_LENGTH)
			list = list.subList(0, Constant.NEW_LIST_LENGTH);
		List<Hardware> returnList = Changer.toHardwareList(list);
		
		return returnList;
	}

	@Override
	public List<Hardware> getHottestList() {
		Session sess = sessionFactory.getCurrentSession();
		String sql = "FROM Radiator WHERE STATE=882";
		List<Radiator> list = sess.createQuery(sql).list();

		Collections.sort(list);

		if (list.size() > Constant.HOT_LIST_LENGTH)
			list = list.subList(0, Constant.HOT_LIST_LENGTH);
		List<Hardware> returnList = Changer.toHardwareList(list);
		
		return returnList;
	}

	@Override
	public List<Brand> getBrandList() {
		Session sess = sessionFactory.getCurrentSession();
		String sql = "FROM BRD_RAD";
		List<Brand> returnList = new ArrayList<Brand>();
		List<BRD_RAD> list = sess.createQuery(sql).list();

		for (int i = 0; i < list.size(); i++) {
			returnList.add(list.get(i));
		}

		
		return returnList;
	}

	@Override
	public List<Radiator> filter(int page, Map<String, List<Integer>> map,
			SortType st, SortWay sw, HardwareSet hs) {
		// 搜索时的过滤方法
		if (map==null || map.size()==0)
			return getRadiatorList(page, st, hs, sw);

		// 获得此产品表
		List<Radiator> hwList = getFilterList(hs, st, sw);

		// 最后的匹配表
		List<Radiator> matchList = new ArrayList<Radiator>();

		// 开始过滤
		Set<String> keys = map.keySet();
		int i = 0;
		for (String s : keys) {
			i++;
			if (s.equals(Constant.radItemNameList[0])) {
				// 品牌过滤
				List<Integer> list = map.get(s);

				for (int j = 0; j < hwList.size(); j++) {
					for (int k = 0; k < list.size(); k++) {
						if (hwList.get(j).getBrand().getBrand()
								.equals(getBrandById(list.get(k)).getBrand())) {
							matchList.add(hwList.get(j));
							break;
						}
					}
				}
			} else if (s.equals(Constant.radItemNameList[1])) {
				List<Integer> list = map.get(s);

				for (int j = 0; j < hwList.size(); j++) {
					for (int k = 0; k < list.size(); k++) {
						if (Changer.checkPrice(hwList.get(j),
								Constant.RADIATOR_PRICE[list.get(k) - 1]
										.getName())) {
							matchList.add(hwList.get(j));
							break;
						}
					}
				}
			} else if (s.equals(Constant.radItemNameList[2])) {
				List<Integer> list = map.get(s);

				for (int j = 0; j < hwList.size(); j++) {
					for (int k = 0; k < list.size(); k++) {
						if (hwList.get(j).getRGMaterial().getValue()
								.equals(getValueByID(list.get(k)).getValue())) {
							matchList.add(hwList.get(j));
							break;
						}
					}
				}
			} else if (s.equals(Constant.radItemNameList[3])) {
				List<Integer> list = map.get(s);

				for (int j = 0; j < hwList.size(); j++) {
					for (int k = 0; k < list.size(); k++) {
						if (hwList.get(j).getHeatEmissionFinMaterial()
								.getValue()
								.equals(getValueByID(list.get(k)).getValue())) {
							matchList.add(hwList.get(j));
							break;
						}
					}
				}
			} else if (s.equals(Constant.radItemNameList[4])) {
				List<Integer> list = map.get(s);

				for (int j = 0; j < hwList.size(); j++) {
					for (int k = 0; k < list.size(); k++) {
						if (hwList.get(j).getType().getValue()
								.equals(getValueByID(list.get(k)).getValue())) {
							matchList.add(hwList.get(j));
							break;
						}
					}
				}
			}
			if (i != keys.size()) {
				hwList = matchList;
				matchList = new ArrayList<Radiator>();
			}
		}
		int maxNum = Pager.getMax(page);
		int minNum = Pager.getMin(maxNum);
		matchList = new Pager<Radiator>()
				.getPageList(matchList, minNum, maxNum);
		return matchList;
	}

	private List<Radiator> getFilterList(HardwareSet hs, SortType st,
			SortWay sw) {
		/*
		 * 获取用于筛选的硬件列表 此列表可以由action提供 或由本方法从数据库获取
		 */
		List<Radiator> l = new ArrayList<Radiator>();
		Session sess = sessionFactory.getCurrentSession();
		String sql = null;

		switch (st) {
		case PRICE:
			
				if (sw == SortWay.ASC || sw == SortWay.DEFAULT)
					sql = "FROM Radiator WHERE STATE=882 ORDER BY OPRICE ASC";
				else
					sql = "FROM Radiator WHERE STATE=882 ORDER BY OPRICE DESC";
				l = sess.createQuery(sql).list();
			
			break;
		case W_SCORE:
			
				if (sw == SortWay.DESC || sw == SortWay.DEFAULT)
					sql = "FROM Radiator WHERE STATE=882 ORDER BY WSCORE DESC";
				else
					sql = "FROM Radiator WHERE STATE=882 ORDER BY WSCORE ASC";
				l = sess.createQuery(sql).list();
			
			break;
		case Z_SCORE:
			
				if (sw == SortWay.DESC || sw == SortWay.DEFAULT)
					sql = "FROM Radiator WHERE STATE=882 ORDER BY ZSCORE DESC";
				else
					sql = "FROM Radiator WHERE STATE=882 ORDER BY ZSCORE ASC";
				l = sess.createQuery(sql).list();
			
			break;
		case NEW:
			
				if (sw == SortWay.DESC || sw == SortWay.DEFAULT)
					sql = "FROM Radiator WHERE STATE=882 ORDER BY INSTORAGETIME DESC";
				else
					sql = "FROM Radiator WHERE STATE=882 ORDER BY INSTORAGETIME ASC";
				l = sess.createQuery(sql).list();
			
			break;
		case HOT:
			
				sql = "FROM Radiator WHERE STATE=882";
				l = sess.createQuery(sql).list();
				if (sw == SortWay.DESC || sw == SortWay.DEFAULT)
					Collections.sort(l);
				else {
					Collections.sort(l, new SortByHotAsc());
				}
			
			break;
		case PERFORMANCE:
			if (sw == SortWay.DESC || sw == SortWay.DEFAULT)
				sql = "FROM Radiator WHERE STATE=882 ORDER BY PERFORMANCE DESC";
			else
				sql = "FROM Radiator WHERE STATE=882 ORDER BY PERFORMANCE ASC";
			l = sess.createQuery(sql).list();
			break;
		default:
			break;

		}
		
		l=uniFilter(hs, l);
		return l;
	}

	@Override
	public List<Radiator> search(String keywords, int page) {
		Session sess = sessionFactory.getCurrentSession();

		int maxNum = Pager.getMax(page);
		int minNum = Pager.getMin(maxNum);

		String sql = "FROM Radiator WHERE STATE=882";
		String keys[] = keywords.split(" ");
		for (int i = 0; i < keys.length; i++) {
			if (i == 0) {
				sql = sql + " WHERE MODEL LIKE '%" + keys[i] + "%'";
			} else {
				sql = sql + " OR MODEL LIKE '%" + keys[i] + "%'";
			}
		}
		List<Radiator> list = sess.createQuery(sql).list();

		List<Radiator> returnList = new Pager<Radiator>().getPageList(list,
				minNum, maxNum);

		
		return returnList;
	}

	@Override
	public HashMap<String, List<Item>> getFilterBoard() {
		HashMap<String, List<Item>> map = new HashMap<String, List<Item>>();
		map.put(getBrandItem().getItemName(), getFilterBrandItem());
		map.put(getPriceItem().getItemName(), getFilterPriceItem());

		for (int i = 2; i < Constant.radItemNameList.length; i++) {
			map.put(Constant.radItemNameList[i], getFilterItemByNum(i));
		}

		return map;
	}

	private List<Item> getFilterItemByNum(int i) {
		Session sess = sessionFactory.getCurrentSession();

		String sql = null;
		List<Value> values = null;
		List<Item> choices = null;

		switch (i) {
		case 2:
			// 热管材质

			// itemChoiceList
			choices = new ArrayList<Item>();

			sql = "FROM Value WHERE KEY>=786 AND KEY<=790 ORDER BY VALUE";
			values = sess.createQuery(sql).list();

			for (int j = 0; j < values.size(); j++) {
				choices.add(new Item(values.get(j).getValue(), values.get(j)
						.getKey()));
			}

			break;
		case 3:
			// 散热片材质

			// itemChoiceList
			choices = new ArrayList<Item>();

			sql = "FROM Value WHERE KEY>=791 AND KEY<=795 ORDER BY VALUE";
			values = sess.createQuery(sql).list();

			for (int j = 0; j < values.size(); j++) {
				choices.add(new Item(values.get(j).getValue(), values.get(j)
						.getKey()));
			}

			break;
		case 4:
			// 类型

			// itemChoiceList
			choices = new ArrayList<Item>();

			sql = "FROM Value WHERE KEY>=746 AND KEY<=760 ORDER BY VALUE";
			values = sess.createQuery(sql).list();

			for (int j = 0; j < values.size(); j++) {
				choices.add(new Item(values.get(j).getValue(), values.get(j)
						.getKey()));
			}

			break;
		default:
			break;

		}
		
		return choices;
	}

	private FilterItem getItemByNum(int i) {
		Session sess = sessionFactory.getCurrentSession();
		FilterItem fi = new FilterItem();
		// itemName
		fi.setItemName(Constant.radItemNameList[i]);

		String sql = null;
		List<Value> values = null;
		List<String> choices = null;

		switch (i) {
		case 2:
			// 热管材质

			// itemChoiceList
			choices = new ArrayList<String>();

			sql = "FROM Value WHERE KEY>=786 AND KEY<=790 ORDER BY VALUE";
			values = sess.createQuery(sql).list();

			for (int j = 0; j < values.size(); j++) {
				choices.add(values.get(j).getValue());
			}

			break;
		case 3:
			// 散热片材质

			// itemChoiceList
			choices = new ArrayList<String>();

			sql = "FROM Value WHERE KEY>=791 AND KEY<=795 ORDER BY VALUE";
			values = sess.createQuery(sql).list();

			for (int j = 0; j < values.size(); j++) {
				choices.add(values.get(j).getValue());
			}

			break;
		case 4:
			// 类型

			// itemChoiceList
			choices = new ArrayList<String>();

			sql = "FROM Value WHERE KEY>=746 AND KEY<=760 ORDER BY VALUE";
			values = sess.createQuery(sql).list();

			for (int j = 0; j < values.size(); j++) {
				choices.add(values.get(j).getValue());
			}

			break;
		default:
			break;

		}
		fi.setItemChoiceList(choices);
		
		return fi;
	}

	private FilterItem getPriceItem() {
		// 价格项
		FilterItem fi = new FilterItem();
		fi.setItemName(Constant.radItemNameList[1]);

		List<String> choices = new ArrayList<String>();
		for (int i = 0; i < (Constant.RADIATOR_PRICE.length); i++) {
			choices.add(Constant.RADIATOR_PRICE[i].getName());
		}

		fi.setItemChoiceList(choices);

		return fi;
	}

	private FilterItem getBrandItem() {
		Session sess = sessionFactory.getCurrentSession();
		FilterItem fi = new FilterItem();
		fi.setItemName(Constant.radItemNameList[0]);

		String sql = "FROM BRD_RAD";
		List<BRD_RAD> brandList = sess.createQuery(sql).list();
		List<String> choices = new ArrayList<String>();

		for (int i = 0; i < brandList.size(); i++) {
			choices.add(brandList.get(i).getBrand());
		}

		fi.setItemChoiceList(choices);
		
		return fi;
	}

	@Override
	public Radiator getOneById(int id) {
		Session sess = sessionFactory.getCurrentSession();
		Radiator c = (Radiator) sess.load(Radiator.class, id);
		
		return c;
	}

	@Override
	public Map<String, List<String>> getInsertboard() {
		// 返回的map
		Map<String, List<String>> rMap = new HashMap<String, List<String>>();

		Session sess = sessionFactory.getCurrentSession();
		String sql = null;
		List<String> l = null;
		List<Value> valueList = null;

		// 品牌
		sql = "FROM BRD_RAD";
		l = new ArrayList<String>();
		List<BRD_RAD> brandList = sess.createQuery(sql).list();
		for (int i = 0; i < brandList.size(); i++) {
			l.add(brandList.get(i).getBrand());
		}
		rMap.put(Constant.radSelectStr[0], l);

		// Value
		// 热管材质
		rMap.put(Constant.radSelectStr[3], getValueByKey(786, 790));

		// 散热片材质
		rMap.put(Constant.radSelectStr[4], getValueByKey(791, 795));

		// 类型
		rMap.put(Constant.radSelectStr[1], getValueByKey(746, 760));

		// 轴承类型
		l = getValueByKey(771, 785);
		rMap.put(Constant.radSelectStr[2], l);

		// 一对多

		// 支持CPU类型
		l = new ArrayList<String>();
		sql = "FROM Interface WHERE RE_RAD=1 AND RE_CPU=1 ORDER BY INTF_TYPE,NAME";
		List<Interface> list3 = sess.createQuery(sql).list();
		for (int i = 0; i < list3.size(); i++) {
			l.add(list3.get(i).getName());
		}
		rMap.put(Constant.radComboBoxStr[0], l);

		// 散热方式
		l = new ArrayList<String>();
		sql = "FROM Value WHERE KEY>=761 AND KEY<=770 ORDER BY VALUE";
		List<Value> list2 = sess.createQuery(sql).list();
		for (int i = 0; i < list2.size(); i++) {
			l.add(list2.get(i).getValue());
		}
		rMap.put(Constant.radComboBoxStr[1], l);

		
		return rMap;
	}

	@Override
	public boolean insertHardware(Map<String, List<String>> map) {
		try {
			// TODO Auto-generated method stub
			Session sess = sessionFactory.getCurrentSession();
			Radiator rad = new Radiator();
			// 本方法处理外键和专属属性，通用属性由其他方法提供
			// radiator有一对多的情况
			Iterator i = map.keySet().iterator();
			// 一对多的list
			List<String> radWay = null;
			List<String> radCompatibleCPU = null;
			while (i.hasNext()) {
				String key = i.next().toString();
				List<String> list = map.get(key);
				String sql = null;
				if (key.equals(Constant.radInforStr[0])) {
					rad.setModel(list.get(0));
					// 外键
				} else if (key.equals(Constant.radSelectStr[0])) {
					sql = "FROM BRD_RAD WHERE BRAND='" + list.get(0) + "'";
					List<BRD_RAD> bl = sess.createQuery(sql).list();
					rad.setBrand(bl.get(0));
				} else if (key.equals(Constant.radSelectStr[1])) {
					rad.setType(getValueByName(list.get(0)));
				} else if (key.equals(Constant.radSelectStr[2])) {
					rad.setBearingType(getValueByName(list.get(0)));
				} else if (key.equals(Constant.radSelectStr[3])) {
					rad.setRGMaterial(getValueByName(list.get(0)));
				} else if (key.equals(Constant.radSelectStr[4])) {
					rad.setHeatEmissionFinMaterial(getValueByName(list.get(0)));
				} else if (key.equals(Constant.radInforStr[1])) {
					// 非外键
					rad.setHighestSpeed(Integer.parseInt(list.get(0)));
				} else if (key.equals(Constant.radInforStr[2])) {
					rad.setSpeedDescription(list.get(0));
				} else if (key.equals(Constant.radInforStr[3])) {
					rad.setNoise(Double.parseDouble(list.get(0)));
				} else if (key.equals(Constant.radInforStr[4])) {
					rad.setRGNUM(Integer.parseInt(list.get(0)));
				} else if (key.equals(Constant.radInforStr[5])) {
					rad.setrSize(list.get(0));
				} else if (key.equals(Constant.radInforStr[6])) {
					rad.setrWeight(Double.parseDouble(list.get(0)));
				} else if (key.equals(Constant.radInforStr[7])) {
					rad.setIntelligentTemControl(Integer.parseInt(list.get(0)));
				} else if (key.equals(Constant.radInforStr[8])) {
					rad.setPowerParameter(list.get(0));
				} else if (key.equals(Constant.radInforStr[9])) {
					rad.setAttachment(list.get(0));
				} else if (key.equals(Constant.radComboBoxStr[1])) {
					// 一对多
					radWay = list;
				} else if (key.equals(Constant.radComboBoxStr[0])) {
					radCompatibleCPU = list;
				} else {
					// 通用属性
					Changer.setUniAttribute(rad, key, list);
				}

			}
			// 默认属性
			Changer.setDefaultAttribute(rad);
			rad.setState(getValueByID(Constant.HW_DEFAULT_STATE));
			
			rad.setPerformance(new RadiatorEvaluator().getPerformance(rad));
//			Transaction t = sess.beginTransaction();
			sess.save(rad);
			// 散热方式表RAD_Way
			for (int j = 0; j < radWay.size(); j++) {
				String s = radWay.get(j);
				Value v = getValueByName(s);

				RAD_Way rw = new RAD_Way();
				rw.setRad(rad);
				rw.setWay(v);
				sess.save(rw);
			}
			// 支持CPU类型表RAD_CompatibleCPU
			for (int j = 0; j < radCompatibleCPU.size(); j++) {
				String s = radCompatibleCPU.get(j);
				Interface v = getInterfaceByName(s);

				RAD_CompatibleCPU radcc = new RAD_CompatibleCPU();
				radcc.setRad(rad);
				radcc.setCpu(v);
				sess.save(radcc);
			}
			sess.flush();
			
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return false;
		}
		return true;
	}

	private List<String> getValueByKey(int i, int j) {
		List<String> rl = new ArrayList<String>();
		Session sess = sessionFactory.getCurrentSession();
		String sql = "FROM Value where KEY>=" + i + " AND KEY<=" + j+" ORDER BY VALUE";
		List<Value> l = sess.createQuery(sql).list();
		for (int k = 0; k < l.size(); k++) {
			rl.add(l.get(k).getValue());
		}
		
		return rl;
	}

	private Value getValueByName(String name) {
		Session sess = sessionFactory.getCurrentSession();
		String sql = "FROM Value WHERE VALUE='" + name + "'";
		List<Value> l = sess.createQuery(sql).list();
		
		return l.get(0);

	}

	private Interface getInterfaceByName(String name) {
		Session sess = sessionFactory.getCurrentSession();
		String sql = "FROM Interface WHERE NAME='" + name + "'";
		List<Interface> l = sess.createQuery(sql).list();
		
		return l.get(0);

	}

	private List<Item> getFilterPriceItem() {
		List<Item> choices = new ArrayList<Item>();
		for (int i = 0; i < (Constant.RADIATOR_PRICE.length); i++) {
			choices.add(Constant.RADIATOR_PRICE[i]);
		}
		return choices;
	}

	private List<Item> getFilterBrandItem() {
		Session sess = sessionFactory.getCurrentSession();

		String sql = "FROM BRD_RAD";
		List<BRD_RAD> brandList = sess.createQuery(sql).list();
		List<Item> choices = new ArrayList<Item>();

		for (int i = 0; i < brandList.size(); i++) {
			choices.add(new Item(brandList.get(i).getBrand(), brandList.get(i)
					.getId()));
		}

		
		return choices;
	}

	private Value getValueByID(Integer integer) {
		Session sess = sessionFactory.getCurrentSession();
		Value c = (Value) sess.load(Value.class, integer);
		
		return c;
	}

	private Interface getInterfaceByID(int id) {
		Session sess = sessionFactory.getCurrentSession();
		Interface c = (Interface) sess.load(Interface.class, id);
		
		return c;

	}

	private BRD_RAD getBrandById(Integer integer) {
		Session sess = sessionFactory.getCurrentSession();
		BRD_RAD c = (BRD_RAD) sess.load(BRD_RAD.class, integer);
		
		return c;
	}

	@Override
	public void save(Radiator t) {
		Session sess = sessionFactory.getCurrentSession();
//		Transaction tran = sess.beginTransaction();
		sess.saveOrUpdate(t);
		sess.flush();
		
	}

	@Override
	public int filterPageNum(Map<String, List<Integer>> map, HardwareSet hs) {

		List<Radiator> hwList = null;
		List<Radiator> matchList = new ArrayList<Radiator>();

		String sql = "FROM Radiator WHERE STATE=882";
		Session sess = sessionFactory.getCurrentSession();
		hwList = sess.createQuery(sql).list();

		hwList = uniFilter(hs, hwList);

		if (map==null || map.size()==0)
			return hwList.size();
		// 开始过滤
		Set<String> keys = map.keySet();
		int i = 0;
		for (String s : keys) {
			i++;
			if (s.equals(Constant.radItemNameList[0])) {
				// 品牌过滤
				List<Integer> list = map.get(s);

				for (int j = 0; j < hwList.size(); j++) {
					for (int k = 0; k < list.size(); k++) {
						if (hwList.get(j).getBrand().getBrand()
								.equals(getBrandById(list.get(k)).getBrand())) {
							matchList.add(hwList.get(j));
							break;
						}
					}
				}
			} else if (s.equals(Constant.radItemNameList[1])) {
				List<Integer> list = map.get(s);

				for (int j = 0; j < hwList.size(); j++) {
					for (int k = 0; k < list.size(); k++) {
						if (Changer.checkPrice(hwList.get(j),
								Constant.RADIATOR_PRICE[list.get(k) - 1]
										.getName())) {
							matchList.add(hwList.get(j));
							break;
						}
					}
				}
			} else if (s.equals(Constant.radItemNameList[2])) {
				List<Integer> list = map.get(s);

				for (int j = 0; j < hwList.size(); j++) {
					for (int k = 0; k < list.size(); k++) {
						if (hwList.get(j).getRGMaterial().getValue()
								.equals(getValueByID(list.get(k)).getValue())) {
							matchList.add(hwList.get(j));
							break;
						}
					}
				}
			} else if (s.equals(Constant.radItemNameList[3])) {
				List<Integer> list = map.get(s);

				for (int j = 0; j < hwList.size(); j++) {
					for (int k = 0; k < list.size(); k++) {
						if (hwList.get(j).getHeatEmissionFinMaterial()
								.getValue()
								.equals(getValueByID(list.get(k)).getValue())) {
							matchList.add(hwList.get(j));
							break;
						}
					}
				}
			} else if (s.equals(Constant.radItemNameList[4])) {
				List<Integer> list = map.get(s);

				for (int j = 0; j < hwList.size(); j++) {
					for (int k = 0; k < list.size(); k++) {
						if (hwList.get(j).getType().getValue()
								.equals(getValueByID(list.get(k)).getValue())) {
							matchList.add(hwList.get(j));
							break;
						}
					}
				}
			}
			if (i != keys.size()) {
				hwList = matchList;
				matchList = new ArrayList<Radiator>();
			}
		}
		
		return matchList.size();

	}

	public int getListPageNum(HardwareSet hs) {
		Session sess = sessionFactory.getCurrentSession();
		String sql = "FROM Radiator WHERE STATE=882";
		List<Radiator> list = sess.createQuery(sql).list();
		if (hs != null) {
			list = MainBoardFilter(hs, list);
			list = CPUFilter(hs, list);
		}
		
		return list.size();
	}

	private List<Radiator> uniFilter(HardwareSet hs, List<Radiator> list) {
		if (hs != null) {
			list = MainBoardFilter(hs, list);
			list = CPUFilter(hs, list);
		}
		return list;
	}

	@Override
	public List<Radiator> getOneBySupplierID(int id) {
		// TODO Auto-generated method stub
		Session sess = sessionFactory.getCurrentSession();
		String sql = "FROM Radiator where SUPPLIER="+id;
		List<Radiator> list = sess.createQuery(sql).list();
		
		return list;
	}
}
